
const ipcRenderer = window.ipcRenderer;
const head = 'kugou/';
async function getKgHotPlayList(f){


    await ipcRenderer.invoke(head+'list/rank').then((r) => {
        //console.log(r)
        let result = []
        for (let playlist of r.plist.list.info) {
            result.push(dealqResult(playlist))
        }
        //console.log("@@@kg",result.length)

        f(result)
    }).catch(() => {
    
        return new Promise((resolve, reject) => {
            reject('err');
        })

    })
}

function dealqResult(data){
    let {specialname:name,specialid:id,imgurl:imgUrl,username : auth} = data;



    return {
        "name":name,
        "id":id,
        "imgUrl":imgUrl,
        "auth":auth,
        "from":'kugou'
    }
}
async function getKgPlayListDetail(id,f){
    await ipcRenderer.invoke(head+'list/detail',{id:id}).then((r) => {

        //console.log(r)
        let result = dealDetailResult(r)
        //console.log(result)
        f(result)
    }).catch(() => {
    
        return new Promise((resolve, reject) => {
            reject('err');
        })

    })
}
function dealDetailResult(data){
    let {info} = data;
    let {list:{nickname:user,imgurl:img,playcount:playCount,collectcount:subscribedCount,specialname:name,specialid:id,intro:description}} = info;
    let tags = info.list.tags.map((i) => {
        return i.tagname
    })
    img = img.replaceAll("{size}","150")
    let tracks = data.list.list.info;
    let r = tracks.map((i) => {
        let name = i.filename.split(" - ");
        let ts = i.duration;
        let min = Math.floor(ts/60);
        let sec = Math.floor(ts - min*60);
        if(min < 10){
            min = '0' + min;
        }
        if(sec < 10){
            sec = '0' + sec;
        }
        let time = `${min} : ${sec}`;
        return{
            'times' : time,
            'name' : name[1],
            'singer' : name[0],
            'album' : i.remark,
            'id' : i.hash,
            from:'kugou'
        }
    })


    return {
        user:user,
        tags:tags,
        name:name,
        id:id,
        description:description,
        img:img,
        playCount:playCount,
        subscribedCount:subscribedCount,
        list:r,
        from:'kugou'
    }
}

async function getKgPlayMusic(id,f){
    await ipcRenderer.invoke(head+'play',{id:id}).then((r) => {

        //console.log(r)
        if(!r.url){
            f(-1)
        }else {
            let result = dealPlayMusicResult(r)
            //console.log(result)
            f(result)
        }
    }).catch(() => {
    
        return new Promise((resolve, reject) => {
            reject('err');
        })

    })
}
/**
 * {
 *                     id: "1",
 *                     title: "打上花火",
 *                     info: "DAOKO,米津玄師",
 *                     // resource: require("./resource/DAOKO,米津玄師 - 打上花火.mp3"),
 *                     resource: "",
 *                     time: "04:50",
 *                     img:
 *                         "http://singerimg.kugou.com/uploadpic/softhead/400/20160913/20160913140233132.jpg"
 *                 },
 */
function dealPlayMusicResult(data){
    let {fileName} = data;
    let s = fileName.replaceAll(' ','');
    let nAndT = s.split('-');

    let {hash: id, album_img,url,timeLength,albumid} = data

    let info = nAndT[0]
    let title = nAndT[1]
    let img = album_img.replaceAll('{size}','150')
    let resource = url;
    let ts = timeLength;
    let min = Math.floor(ts/60);
    let sec = Math.floor(ts - min*60);
    if(min < 10){
        min = '0' + min;
    }
    if(sec < 10){
        sec = '0' + sec;
    }
    let time = `${min} : ${sec}`;

    return {
        id:id,
        title:title,
        info:info,
        resource:resource,
        time:time,
        from:'kugou',
        albumid:albumid,
        img:img
    }
}

async function getKgSearch(keywords,page,pagesize,f){
    await ipcRenderer.invoke(head+'search',{keyword:keywords,page:page,pagesize:pagesize}).then((r) => {

        //console.log(r)
        let result = dealSearchResult(r)
        let count = r?.total || 0;
        f({v:result,count:count})
    }).catch(() => {
    
        return new Promise((resolve, reject) => {
            reject('err');
        })

    })
}
function dealSearchResult(data) {

    let tracks = data.data.info;
    let r = tracks.map((i) => {
        let ts = i.duration;
        let min = Math.floor(ts/60);
        let sec = Math.floor(ts - min*60);
        if(min < 10){
            min = '0' + min;
        }
        if(sec < 10){
            sec = '0' + sec;
        }
        let time = `${min} : ${sec}`;
        return{
            'times' : time,
            'name' : i.songname,
            'singer' : i.singername,
            'album' : i.album_name,
            'id' : i.hash,
            'from' : 'kugou'
        }
    })
    return r
}
async function getKglyric(id,albumid,f){
    await ipcRenderer.invoke(head+'lrc',{id:id,albumid:albumid}).then((r) => {
       //console.log(r)
        let result = deallyricResult(r)
        f(result.replaceAll('\r',''))
    }).catch(() => {
    
        return new Promise((resolve, reject) => {
            reject('err');
        })

    })
}
function deallyricResult(data){

    let list = data.data.lyrics || ""


    return list;

}


export {getKgHotPlayList,getKgPlayListDetail,getKgPlayMusic,getKgSearch,getKglyric}